Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(rust, python): treat null columns as zero in sum_horizontal #13880

Merged
merged 6 commits into from
Jan 22, 2024

Conversation

edavisau
Copy link
Contributor

Fixes #13113

  • Null columns are treated as additive identity in the presence of non-null columns. They can be skipped in the computation
  • If all columns are Null, return Null

I have also changed the behaviour of single columns sums. This seems more consistent to multi-column sums. Example:

    df = pl.DataFrame(
        {
            "A": [5, None, 3, None],
            "B": [5, 3, None, None],
        }
    )

As reference, when summing multiple columns
sum_horizontal("A", "B") = [10, 3, 3, 0]

Before
sum_horizontal("A") = [5, None, 3, None]
After
sum_horizontal("A") = [5, 0, 3, 0]

@github-actions github-actions bot added fix Bug fix python Related to Python Polars rust Related to Rust Polars labels Jan 20, 2024
Copy link
Member

@stinodego stinodego left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR!

If all columns are Null, return Null

I don't think this is correct. Null should just be treated as the identity always.

@edavisau
Copy link
Contributor Author

@stinodego In this case I was referring to the actual Null data type, as opposed to all values being null i.e.

df = pl.DataFrame([
    pl.Series("A", [None, None, None], pl.Null),
    pl.Series("B", [None, None, None], pl.Null),
    pl.Series("C", [None, None, None], pl.Null),
])

If you replaced pl.Null with pl.Int32 above you'll get 0 in the result, similarly if you replaced with pl.Utf8 you'll get empty strings in the result. In addition, if 2 of the columns were pl.Null, and one was pl.Int32, you'd still get a pl.Int32 result. The all-pl.Null situation seemed too ambiguous though. Do you have another suggestion for this case?

Thanks!

@stinodego
Copy link
Member

Ah, I hadn't understood that part. Then it's probably fine!

Copy link
Member

@ritchie46 ritchie46 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@ritchie46 ritchie46 merged commit 42a4231 into pola-rs:main Jan 22, 2024
22 checks passed
r-brink pushed a commit to r-brink/polars that referenced this pull request Jan 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fix Bug fix python Related to Python Polars rust Related to Rust Polars
Projects
None yet
Development

Successfully merging this pull request may close these issues.

not yet implemented when call pl.sum_horizontal
3 participants